<?php

/**
 * account actions.
 *
 * @package    blogallery
 * @subpackage account
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class accountActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex($request)
  {
    $this->forward('account', 'login');
  }
  
  public function executeLogin($request)
  {
    if ($this->getUser()->isAuthenticated())
    {
      $this->redirect('@homepage');
    }
    
    $form = new LoginForm();
    
    if ($request->isMethod('post'))
    {
      $form->bind($request->getParameter('login'));
      
      if ($form->isValid())
      {
      	$user = UserPeer::retrieveByUsername($form->getValue('username'));
        $this->getUser()->signIn($user);        
        return $this->redirect('@default?module=user&action=index');
      }    
    }
    
    $this->form = $form;
  }
  
  public function executeLogout()
  {
    $this->getUser()->signOut();
    return $this->redirect('@homepage');  	
  }
    
  public function executeCreate($request)
  {
  	//if ($request->getParameter('key') === sfConfig::get('app_key_create'))
  	if ($request)
  	{
  	  $form = new UserForm();
  	 
  	  $this->getResponse()->addJavascript('livepipe.js', 'last');
  	  $this->getResponse()->addJavascript('tabs.js', 'last'); 

  	  $this->form = $form;
  	} 
  	else
  	{
  	  return $this->redirect('@homepage'); 	
  	}
  }
  
  public function executeEdit($request)
  {
  	$user = UserPeer::retrieveByPK($this->getUser()->getId());
  	$this->forward404Unless($user instanceof User);  
  		
  	$form = new UserForm($user);
  	  		
  	$this->getResponse()->addJavascript('livepipe.js', 'last');
  	$this->getResponse()->addJavascript('tabs.js', 'last');
  	 
  	$this->form = $form;  	
  }
  
  public function executeUpdate($request)
  {
  	if ($request->isMethod('post'))
  	{
  	  if ($request->getParameter('user[id]'))
  	  {
  	    if ($request->getParameter('user[id]') !== $this->getUser()->getId())
  	    {
  	      return $this->redirect('@homepage');
  	    }
  	
  	  	$user = UserPeer::retrieveByPK($request->getParameter('user[id]'));
  	  	$this->forward404Unless($user instanceof User);
  	    $form = new UserForm($user); 
  	    $redirect = '@default?module=user&action=index'; 
  	    $this->setTemplate('edit');	  
  	  }
  	  else
  	  {
  	  	$user = new User();
  	    $form = new UserForm($user);  	  
  	    $redirect = '@default?module=user&action=edit'; 	  
  	    $this->setTemplate('create');	  
  	  }
  	  
  	  $form->bind($request->getParameter('user'), $request->getFiles('user'));
  	  
  	  if ($form->isValid())
  	  {
  	  	$form->save();
  	  	
  	  	// refresh user details or sign in the new user
  	  	$this->getUser()->signIn($form->getObject());
  	  	
  	  	return $this->redirect($redirect);
  	  }
  	  else
  	  {
  	  	$this->form = $form;
  	  }
  	} 

  	$this->forward404();
  }
}
